package com.ruoyi.xyq.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.xyq.domain.Withdrawal;
import com.ruoyi.xyq.service.IWithdrawalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.xyq.domain.Details;
import com.ruoyi.xyq.service.IDetailsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 收支记录Controller
 * 
 * @author ruoyi
 * @date 2025-05-19
 */
@RestController
@RequestMapping("/service-details")
public class DetailsController extends BaseController
{
    @Autowired
    private IDetailsService detailsService;

    @Autowired
    private IWithdrawalService withdrawalService;
    /**
     * 查询收支记录列表
     */
    @RequiresPermissions("xyq:details:list")
    @GetMapping("/lists")
    public TableDataInfo list(Details details)
    {
        startPage();
        List<Details> list = detailsService.selectDetailsList(details);
        return getDataTable(list);
    }

    /**
     * 导出收支记录列表
     */
    @RequiresPermissions("xyq:details:export")
    @Log(title = "收支记录", businessType = BusinessType.EXPORT)
    @PostMapping("/exports")
    public void export(HttpServletResponse response, Details details)
    {
        List<Details> list = detailsService.selectDetailsList(details);
        ExcelUtil<Details> util = new ExcelUtil<Details>(Details.class);
        util.exportExcel(response, list, "收支记录数据");
    }

    /**
     * 获取收支记录详细信息
     */
    @RequiresPermissions("xyq:details:query")
    @GetMapping(value = "/{id}") // 修改路径
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(detailsService.selectDetailsById(id));
    }

    /**
     * 新增收支记录
     */
    @RequiresPermissions("xyq:details:add")
    @Log(title = "收支记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Details details)
    {
        //添加明细表
        int i = detailsService.insertDetails(details);
        //获取提现表信息
        Withdrawal withdrawal1 = withdrawalService.selectWithdrawalById(details.getMerchantId());
        //增加当前余额
        withdrawal1.setCurrentBalance(withdrawal1.getCurrentBalance()+details.getIncome());
        //更新提现表
        int i1 = withdrawalService.updateWithdrawal(withdrawal1);
        if(i>0&&i1>0){
            return success("添加成功");
        }
        return AjaxResult.error("失败");
    }

    /**
     * 修改收支记录
     */
    @RequiresPermissions("xyq:details:edits")
    @Log(title = "收支记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Details details)
    {
        return toAjax(detailsService.updateDetails(details));
    }

    /**
     * 删除收支记录
     */
    @RequiresPermissions("xyq:details:removes")
    @Log(title = "收支记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(detailsService.deleteDetailsByIds(ids));
    }
}
